<template>
	<view>
		<view class="goods-item">
			<!-- 商品左侧 -->
			<view class="goods-left-box">
				<radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandler"></radio>
				<image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
			</view>
			<!-- 商品右侧 -->
			<view class="goods-right-box">
				<view class="goods-title">{{ goods.goods_name}}</view>
				<view class="goods-info">
					<view class="goods-price">￥{{ goods.goods_price | tofixed}}</view>
					<uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numChange"></uni-number-box>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"my-goods",
		data() {
			return {
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
			};
		},
		props:{
			goods:{
				type:Object,
				default:{}
			},
			showRadio:{
				type:Boolean,
				default:false
			},
			showNum:{
				type:Boolean,
				default:false
			}
		},
		methods:{
			radioClickHandler(){
				this.$emit('radio-change',{ 
					goods_id:this.goods.goods_id,
					goods_state: !this.goods.goods_state})
			},
			// val即为value最新的数据
			numChange(val){
				this.$emit('num-change',{
					goods_id: this.goods.goods_id,
					// 确保val的值为数字类型，因为uni-number-box组件可以输入值更改商品数量
					goods_count: +val //等价于val - 0
				})
			}
		},
		filters:{
			tofixed(num){
				return Number(num).toFixed(2)
			}
		}
	}
</script>

<style lang="scss">
.goods-item{
	display: flex;
	padding: 10px 5px;
	border-bottom: 1px solid #f0f0f0;
	border-radius: 15px;
}
.goods-pic{
	display: block;
	width: 100px;
	height: 100px;
	
}
.goods-left-box{
	margin-right: 5px;
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.goods-right-box{
	display: flex;
	flex: 1;
	flex-direction: column;
	justify-content:space-between;
}
.goods-title{
	font-size: 13px;
}
.goods-price{
	font-size: 16px;
	color: #c00000;
}
.goods-info{
	display: flex;
	justify-content: space-between;
    align-items: center;
}

</style>
